package com.linxianqin.springbootVue.manage.Async;

import com.linxianqin.springbootVue.domain.constant.Constants;
import com.linxianqin.springbootVue.domain.dto.SysLogininforDTO;
import com.linxianqin.springbootVue.domain.dto.SysOperLogDTO;
import com.linxianqin.springbootVue.service.SysLogininforService;
import com.linxianqin.springbootVue.service.SysOperLogService;
import com.linxianqin.springbootVue.utils.LogUtils;
import com.linxianqin.springbootVue.utils.ServletUtils;
import com.linxianqin.springbootVue.utils.SpringUtils;
import com.linxianqin.springbootVue.utils.ip.AddressUtils;
import com.linxianqin.springbootVue.utils.ip.IpUtils;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.TimerTask;

/**
 * 功能描述：
 *
 * @author lxq
 * @version 1.00
 * @Date 2020/12/13
 */
@Slf4j
public class AsyncFactory {

    public static TimerTask recordLogininfor(final String username, final String status,
                                             final String message, final Object... args){
        HttpServletRequest request = ServletUtils.getRequest();
        final UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr(request);
        return new TimerTask() {
            @Override
            public void run() {
                String address = AddressUtils.getRealAddressByIP(ip);
                StringBuilder s = new StringBuilder();
                s.append(LogUtils.getBlock(ip));
                s.append(address);
                s.append(LogUtils.getBlock(username));
                s.append(LogUtils.getBlock(status));
                s.append(LogUtils.getBlock(message));
                // 打印信息到日志
                log.info(s.toString(), args);
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                SysLogininforDTO logininforDTO = new SysLogininforDTO();
                logininforDTO.setUserName(username);
                logininforDTO.setIpaddr(ip);
                logininforDTO.setLoginLocation(address);
                logininforDTO.setBrowser(browser);
                logininforDTO.setOs(os);
                logininforDTO.setMsg(message);
                // 日志状态
                if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status)) {
                    logininforDTO.setStatus(Constants.SUCCESS);
                } else if (Constants.LOGIN_FAIL.equals(status)) {
                    logininforDTO.setStatus(Constants.FAIL);
                }
                // 插入数据
                SpringUtils.getBean(SysLogininforService.class).insertLogininfor(logininforDTO);
            }
        };
    }
    /**
     * 操作日志记录
     *
     * @param operLogDTO 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOper(SysOperLogDTO operLogDTO) {
        return new TimerTask() {
            @Override
            public void run() {
                // 远程查询操作地点
                operLogDTO.setOperLocation(AddressUtils.getRealAddressByIP(operLogDTO.getOperIp()));
                SpringUtils.getBean(SysOperLogService.class).insertOperlog(operLogDTO);
            }
        };
    }
}
